###### Figure A2 Script #####

rm(list=ls());gc();gc();gc();gc();gc();gc();gc();gc()

library(tidyverse)
library(lubridate)
library(hrbrthemes)
library(scales)
library(RColorBrewer)
library(viridis)


here::i_am("Scripts/FigA2Script.R")

library(here)




#Aggregate each type of contribution for each state at the year level

#KY
dfky<-readRDS(here("Data","KYstatecontribs.rds"))

#Get year totals since 1996 and before 2019
dfky2<-dfky%>%
  filter(Office_Sought=="STATE REPRESENTATIVE"|Office_Sought=="SLATE"|Office_Sought=="STATE SENATOR "
         |Office_Sought=="ATTORNEY GENERAL"|Office_Sought=="SUPREME COURT JUDGE"
         |Office_Sought=="SECRETARY OF STATE"|Office_Sought=="COMMISSIONER OF AGRICULTURE"
         |Office_Sought=="COURT OF APPEALS JUDGE"|Office_Sought=="STATE TREASURER")

dfky2$type<-ifelse(dfky2$Transaction_Category=="INDIVIDUAL", "Individual", "Other")

kyyeartot<-dfky2 %>%
  filter(year>1995 & year<2020) %>%
  group_by(year, type) %>%
  dplyr::summarize(numcons=n())

kyyeartot$state<-"KY"

#saveRDS(kyyeartot, "KYcontsagg.rds")


kyyearevents<-dfky2 %>%
  filter(year>1995 & year<2019 &fundraiser==1) %>%
  group_by(year, type) %>%
  dplyr::summarize(numcons=n())

kyyearevents$state<-"KY"

#saveRDS(kyyearevents, "KYfundraiseragg.rds")


#MI

dfmi<-readRDS(here("Data","MIstatecontribs.rds"))
dfmi$fundraiser<-0
dfmi$fundraiser[grepl("DIRECT/FUND RAISER  ", dfmi$contribtype)]<-1
dfmi$year<-as.numeric(dfmi$yeardate)
dfmi$type<-ifelse(dfmi$f_name=="", "Other", "Individual")

#Get year totals since 1996 and before 2019
miyeartot<-dfmi %>%
  filter(year>1995 & year<2019) %>%
  group_by(year, type) %>%
  dplyr::summarize(numcons=n())

miyeartot$state<-"MI"

#saveRDS(miyeartot, "MIcontsagg.rds")


miyearevents<-dfmi %>%
  filter(year>1995 & year<2019 &fundraiser==1) %>%
  group_by(year, type) %>%
  dplyr::summarize(numcons=n())

miyearevents$state<-"MI"

#saveRDS(miyearevents, "MIfundraiseragg.rds")


#OH
dfOH<-readRDS(here("Data","OHstatecontribs.rds"))
dfOH$year<-as.numeric(dfOH$yeardate)
dfOH$type<-ifelse(dfOH$NON_INDIVIDUAL==" ", "Individual", "Other")

#Get year totals since 1996 and before 2019
OHyeartot<-dfOH %>%
  filter(year>1995 & year<2019) %>%
  group_by(year, type) %>%
  dplyr::summarize(numcons=n())

OHyeartot$state<-"OH"

#saveRDS(OHyeartot, "OHcontsagg.rds")


OHyearevents<-dfOH %>%
  filter(year>1995 & year<2019 &fundraiser==1) %>%
  group_by(year, type) %>%
  dplyr::summarize(numcons=n())

OHyearevents$state<-"OH"

#saveRDS(OHyearevents, "OHfundraiseragg.rds")

#WV

dfWV<-readRDS(here("Data","WVstatecontribs.rds"))

dfWV$fundraiser<-ifelse(dfWV$Type_of_Transaction=="E", 1, 0)
dfWV$realdate<-ymd(dfWV$Date)
dfWV$year<-year(dfWV$realdate)
dfWV$type<-ifelse(dfWV$Type_of_Contributor=="Individual", "Individual", "Other")

#Get year totals since 1996 and before 2019
WVyeartot<-dfWV %>%
  filter(year>1996 & year<2019) %>%
  group_by(year, type) %>%
  dplyr::summarize(numcons=n())

WVyeartot$state<-"WV"

#saveRDS(WVyeartot, "WVcontsagg.rds")


WVyearevents<-dfWV %>%
  filter(year>1997 & year<2019 &fundraiser==1) %>%
  group_by(year, type) %>%
  dplyr::summarize(numcons=n())

WVyearevents$state<-"WV"

#saveRDS(WVyearevents, "WVfundraiseragg.rds")



#Combine all these to draw graphs
#miyeartot<-readRDS("MIcontsagg.rds")
#OHyeartot<-readRDS("OHcontsagg.rds")
#kyyeartot<-readRDS("KYcontsagg.rds")
#WVyeartot<-readRDS("WVcontsagg.rds")

#Totals
totals<-rbind(miyeartot, OHyeartot,kyyeartot,  WVyeartot)




#Graph totals

#Start at 1997:
all1997<-totals %>%
  filter(year>1996)

all1997$year2<-as.Date(paste(as.character(all1997$year), "01", "01", sep="-"))


ggplot(data=all1997, aes(x=year2)) +
  geom_point( aes(y=numcons, shape=type), size=6) + 
  scale_shape_manual(values = c(17, 3))+
  scale_y_continuous(
    trans="log10",
    # Features of the first axis
    name = "Number of Contributions (Total)",
    
    # Add a second axis and specify its features
    labels=comma
  ) + 
  theme_ipsum() +
  theme(
    axis.text.x=element_text(size=20),
    axis.text.y=element_text(size=20),
    axis.title.y = element_text(size=20),
    axis.title.x = element_text(size=20),
    legend.text=element_text(size=20),
    legend.title=element_text(size=20),
    strip.text.x = element_text(size = 20),
    legend.position="bottom"
  ) +
  scale_x_date(name="Year",breaks = "4 years", 
               date_labels = "%Y")+
  facet_wrap(~ state, nrow = 2, scales = "free_x")+
  labs(colour="State", shape="Type")+
  scale_color_brewer(palette = "Dark2")


ggsave(here("Results", "FigA2.png"), height=12, width=20)
